<template>
  <div id="app">
    <van-notice-bar
        scrollable
        left-icon="volume-o"
        text="欢迎使用课表程序~版权所有：码农权~"
    />
    <van-image
        width="100%"
        height="260"
        src="https://img01.yzcdn.cn/vant/cat.jpeg"
    />
    <van-form @submit="onSubmit" error-message-align="left" submit-on-enter >
      <van-field
          v-model="username"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
          v-model="password"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
    <van-row>
      <van-row @click="register">
        <van-col span="8"></van-col>
        <van-col span="8"></van-col>
        <van-col span="8">还没有账号？</van-col>
      </van-row>
    </van-row>
  </div>
</template>

<script>

export default {
  name: 'userLogin',
  data() {
    return {
      username: '',
      password: '',
    };
  },
  methods: {
    async onSubmit(values) {
      let { data } = await this.$http.post('http://200rx58782.51vip.biz:37899/api/login', {userID: values['用户名'], password: values['密码']});
      console.log(data)
      if(data.status === 200) {
        this.$toast.success('登录成功');
        localStorage.setItem('token', 'HGdsalkjdhksajhdjkas123132');
        setTimeout(()=> {
          this.$router.push('/home')
        }, 300)
      }else if(data.status === 201){
        this.$toast.fail('密码错误')
      }else if(data.status === 404) {
        this.$toast.fail('账号不存在')
      }
    },
    change() {
      console.log('被点击了')
    },
    register() {
      this.$router.push('/Register')
    }
  },
};
</script>

<style scoped>

</style>